package com.example.italkapp2.controller.activity;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.example.italkapp2.R;
import com.example.italkapp2.utils.Constant;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.modules.chat.EaseChatFragment;
import com.hyphenate.easeui.widget.EaseTitleBar;

//会话详情页面
public class ChatActivity extends FragmentActivity {
    private EaseChatFragment chatFragment;
    private String mTitleName;
    private EaseTitleBar tb_chat_conversation_title;
    private int chatType;
    private LocalBroadcastManager mLBM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        //纠正返回mainActivity时候的错误
        if (getIntent().getExtras() == null) {
            finish();
            return;
        }

        initView();
        initData();
        initListener();
    }

    private void initView() {
        tb_chat_conversation_title=findViewById(R.id.tb_chat_conversation_title);
    }

    private void initData() {
        //获取当前聊天类型
        chatType = getIntent().getExtras().getInt(EaseConstant.EXTRA_CHAT_TYPE);

        //根据类型设置名字是群名字还是好友名字,并设置右上角的显示
        if(chatType == EaseConstant.CHATTYPE_GROUP){
            tb_chat_conversation_title.setRightLayoutVisibility(1);
            mTitleName = getGroupName(getIntent().getExtras().getString("titleName"));
        }else {
            tb_chat_conversation_title.setRightLayoutVisibility(-1);
            mTitleName=getIntent().getExtras().getString("titleName");
        }

        //设置标题栏好友名字
        tb_chat_conversation_title.setTitle(mTitleName);
        tb_chat_conversation_title.setLeftLayoutClickListener(v -> ChatActivity.this.finish());

        //创建一个会话fragment
        chatFragment = new EaseChatFragment();

        Bundle bundle = new Bundle();
        bundle.putString(EaseConstant.EXTRA_CONVERSATION_ID, getIntent().getStringExtra(EaseConstant.EXTRA_CONVERSATION_ID));
        bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE, chatType);
        chatFragment.setArguments(bundle);

        //替换fragment
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_chat,chatFragment,"chat").commit();

        //获取发送广播的管理者
        mLBM = LocalBroadcastManager.getInstance(ChatActivity.this);
    }

    private void initListener() {
        //如果是用户的话就不能点击该事件,只有群可以
        if(chatType==EaseConstant.CHATTYPE_GROUP) {
            //标题栏右侧点击事件
            tb_chat_conversation_title.setOnRightClickListener(new EaseTitleBar.OnRightClickListener() {
                @Override
                public void onRightClick(View view) {
                    Intent intent = new Intent(ChatActivity.this, GroupDetailActivity.class);
                    //群id
                    intent.putExtra(Constant.GROUP_ID, mTitleName);
                    intent.putExtra(EaseConstant.EXTRA_CONVERSATION_ID,getIntent().getStringExtra(EaseConstant.EXTRA_CONVERSATION_ID));

                    startActivity(intent);
                }
            });
        }

        //如果当前是群聊类型,就注册广播
        if(chatType==EaseConstant.CHATTYPE_GROUP){

            BroadcastReceiver ExitGroupReceiver=new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if(mTitleName.equals(intent.getStringExtra(Constant.GROUP_ID))){
                        //结束当前页面
                        finish();
                    }
                }
            };
            //注册退群广播
            mLBM.registerReceiver(ExitGroupReceiver,new IntentFilter(Constant.EXIT_GROUP));
        }
    }

    public static String getGroupName(String groupId) {
        EMGroup group = EMClient.getInstance().groupManager().getGroup(groupId);
        if(group == null) {
            return groupId;
        }
        return TextUtils.isEmpty(group.getGroupName()) ? groupId : group.getGroupName();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}